package com.icss.web.head.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.icss.common.controller.BaseController;
import com.icss.common.util.PageInfoUtil;
import com.icss.common.vo.ApiResponse;
import com.icss.common.vo.ApiResponseEnum;
import com.icss.goods.service.IGoodsDbService;
import com.icss.goods.vo.ReEval;
import com.icss.goods.vo.ReEvalEnum;
import com.icss.users.vo.ReUsers;

/**
 * 商品前台操作
 * 
 * @author 刘鑫
 */
@Controller
@RequestMapping("/head/goods/")
public class GoodsHeadController extends BaseController
{
	@Resource
	private IGoodsDbService goodsDbService;
	
	/**
	 * 评论列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/evalList")
	public String evalList(HttpServletRequest request) throws ServletException, IOException
	{
		ReUsers users = (ReUsers) this.findObjfromSession(request, "users");
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		condMap.put("usersId", users.getId());
		condMap.put("status", ReEvalEnum.STATUS_ENABLE.getStatus());
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<ReEval> apiResponse = this.goodsDbService.findCondListEvalService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		return "/head/goods/evalList";
	}

	/**
	 * 打开评论页面
	 * @param request
	 * @param response
	 */
	@RequestMapping("/evalInsert")
	public String evalInsert(HttpServletRequest request) throws ServletException, IOException
	{
		/* 页面跳转 */
		return "/head/goods/evalInsert" ; 
	}
	
	/**
	 * 评论提交
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/evalInsertSubmit")
	public String evalInsertSubmit(HttpServletRequest request,ReEval eval) throws ServletException, IOException
	{
		ReUsers users = (ReUsers) this.findObjfromSession(request, "users");
		
		/* 创建对象 */
		eval.setUsersId(users.getId());
		
		ApiResponse<Object> apiResponseDb = this.goodsDbService.saveOneEvalService(eval);
		if(apiResponseDb.getCode() == ApiResponseEnum.STATUS_SUCCESS.getStatus())
		{
			return "redirect:/head/goods/evalList.htm" ; 
		}
		return this.evalInsert(request);
	}

	/**
	 * 评论更新
	 * @param request
	 * @param response
	 */
	@RequestMapping("/evalUpdate")
	public String evalUpdate(HttpServletRequest request) throws ServletException, IOException
	{
		/* 准备路径 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<ReEval> apiResponse = this.goodsDbService.findOneEvalService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		return "/head/goods/evalInfo" ; 
	}
}
